package com.song.gateway.config;

import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import reactor.core.publisher.Mono;

@Configuration
public class GatewayConfig {
    @Bean("customKeyResolver")
    public KeyResolver customKeyResolver(){
        return new CustomKeyResolver();
    }

    /**
     * IP限流
     * @return
     */
    @Bean("hostAddrKeyResolver")
    public KeyResolver hostAddrKeyResolver(){
        return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getAddress().getHostAddress());
    }

    /**
     * 用户限流
     * @return
     */
    @Bean("userKeyResolver")
    public KeyResolver userKeyResolver(){
        return exchange -> Mono.just(exchange.getRequest().getQueryParams().getFirst("yhid"));
    }

    /**
     * 接口限流
     * @return
     */
    @Bean("apiKeyResolver")
    public KeyResolver apiKeyResolver(){
        return exchange -> Mono.just(exchange.getRequest().getPath().value());
    }
}
